﻿//===============================================================================
// File: ActionHelper.cs
// Author:Veiller Hu
// CLR Version：4.0.30319.1
// Created Date:  2011/2/20 3:20:44 PM 
// Summary:  
//===============================================================================
//
// Copyright (c) East China Information Technology Corporation. All rights reserved.
//
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
// KIND, WHETHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
// PURPOSE.
//===============================================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GPMS.BLL
{
    using GPMS.Extension;
    using GPMS.Model;
    public static class ActionHelper
    {
        public static List<ActionSummary> GetActions(ExtendedGPMSDb db)
        {
            List<string> noVerify = NoVerify.Get();
            List<string> linkless = NoVerify.GetLinklessController();
            linkless.Remove("home");
            return db.ActionInfos
                .Where(a => a.IsDeleted == false && 
                    !noVerify.Contains(a.ActionName.ToLower()) 
                    && !linkless.Contains(a.ControllerInfo.ControllerName.ToLower()))
                .OrderBy(a=>a.ControllerInfo.ControllerName)
                .ThenBy(a=>a.ActionName)
                .Select(a => new ActionSummary
                {
                    ActionID = a.ActionID,
                    Action=a.ActionName,
                    Controller=a.ControllerInfo.ControllerName
                }).ToList();
        }

        public static List<ActionInfo> GetList(ExtendedGPMSDb db)
        {
            return db.ActionInfos.Where(a=>a.IsDeleted==false).ToList();
        }
        public static ActionInfo Get(Guid id,ExtendedGPMSDb db)
        {
            return db.ActionInfos.FirstOrDefault(a => a.ActionID == id);
        }
        public static bool IsExist(Guid id, ExtendedGPMSDb db)
        {
            return db.ActionInfos.Any(a => a.ActionID == id
                && a.IsDeleted == false);
        }
        public static bool IsExist(string actionName,Guid controllerID, ExtendedGPMSDb db)
        {
            return db.ActionInfos.Any(a => a.ActionName == actionName 
                && a.ControllerID == controllerID 
                && a.IsDeleted == false);
        }
        public static Guid Add(ActionInfo actionInfo, ExtendedGPMSDb db)
        {
            if (actionInfo.ActionID==Guid.Empty)
            {
                actionInfo.ActionID = Guid.NewGuid();
            }
            if (IsExist(actionInfo.ActionName,actionInfo.ControllerID,db))
            {
                throw new DuplicateException();
            }
            actionInfo.IsDeleted = false;
            actionInfo.CreateDate = DateTime.Now;
            db.ActionInfos.AddObject(actionInfo);
            db.SaveChanges();
            return actionInfo.ActionID;
        }
        public static void Deleted(Guid id, ExtendedGPMSDb db)
        {
            ActionInfo actionInfo = db.ActionInfos.FirstOrDefault(a => a.ActionID == id);
            actionInfo.IsDeleted = true;
            db.SaveChanges();
        }
    }
}
